﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace DropboxApp
{

    class Program
    {
        static void Main(string[] args)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=56mrkw3ejdx2ucz");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/1/oauth2/token?code=ABCDEF&grant_type=authorization_code");
            request.Method = WebRequestMethods.Http.Get;
            //request.AllowAutoRedirect = false;
            request.Credentials = CredentialCache.DefaultCredentials;

            // Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
            ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.ResponseUri);
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string responseFromServer = reader.ReadToEnd();
            //Console.WriteLine(responseFromServer);
            System.IO.StreamWriter file = new System.IO.StreamWriter("test.html");
            file.WriteLine(responseFromServer);

            file.Close();
            Console.ReadKey();
        }

        void test()
        {
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(
              "http://www.baidu.com");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams and the response.
            reader.Close();
            response.Close();
            Console.ReadKey();
        }
    }
}
